﻿using System.Data;
using HRMSystemShi.Model;
using Microsoft.Data.SqlClient;

namespace HRMSystemShi.DAL
{
    public class DepartmentService
    {
        public static Guid GetDepartmentId(string departmentName)
        {
            string sql = "SELECT Id From Department WHERE Name=@name";
            return (Guid)SqlHelper.ExecuteScalar(sql, new SqlParameter("@name", departmentName));
        }
        public static List<Department> GetDepartmentList()
        {
            List<Department> departments=new List<Department>();
            string sql = "SELECT * FROM Department";
            SqlDataReader reader = SqlHelper.ExecuteReader(sql);
            while (reader.Read())
            {
                Department department = new Department() { Id = (Guid)reader["Id"], Name = reader["Name"].ToString() };
                departments.Add(department);
            }
            return departments;
        }
        public static bool AddDepartment(string deptmentName)
        {
            string sql = "INSERT INTO Department(Id, Name) VALUES(NEWID(), @Name)";
            return SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@Name", deptmentName)) > 0;
        }

        public static bool IsDepartmentExists(string departmentName)
        {
            string sql = "SELECT COUNT(*) FROM Department WHERE Name=@Name";
           return (int)SqlHelper.ExecuteScalar(sql, new SqlParameter("@Name", departmentName))>0;
        }
        public static DataTable GetDepartMentDataTable()
        {
            string sql = "select Id, Name as 部门名 from Department";
            return SqlHelper.GetDataTable(sql);
        }
        public static bool DeleteDepartment(Guid id)
        {
            string sql = "delete from Department where Id=@Id";
            return SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@Id", id))>0;
        }
    }
}
